package client

import (
	"fmt"
	"sanguo/node/moniter"
	"sync"
)

var lock sync.Mutex

var timeInterval int //时间间隔 秒
var count int

type Info struct {
	Name        string //协议名
	Req         int32  //请求数量
	Resp        int32
	TotalTime   int64
	MaxTime     int64
	AverageTime float64
	Count       int64
}

//组数

type Node struct {
	info *Info
	next *Node
}

type Queue struct {
	front *Node
	rear  *Node
}

var NameToQueue map[string]*Queue

func NewInfo(name string) *Info {
	return &Info{
		Name: name,
		Req:  0,
		Resp: 0,
	}
}

func Register(name string) {
	if _, ok := NameToQueue[name]; !ok {
		NameToQueue[name] = &Queue{}
		for i := 0; i < count; i++ {
			NameToQueue[name].PushQueue(&Info{Name: name})
		}
		fmt.Println("Register", name)
	}
}

func (this *Queue) PushQueue(info_ *Info) {
	node := &Node{
		info: info_,
	}
	if nil == this.rear {
		this.front = node
		this.rear = node
	} else {
		this.rear.next = node
		this.rear = node
	}
}

func (this *Queue) PullQueue() {
	if this.front != nil {
		this.front = this.front.next
		if this.front == nil {
			this.front = nil
			this.rear = nil
		}
	}
}

func GetQueue(name string) *Queue {
	var queue *Queue
	var ok bool

	if queue, ok = NameToQueue[name]; !ok {
		//fmt.Println("unregister id:", id)
		return nil
	}

	return queue
}

func Init() {
	timeInterval = 1
	count = 7
	NameToQueue = map[string]*Queue{}

	for _, field := range moniter.Message {
		for _, name := range field {
			Register(name)
		}
	}

}
